import { takeEvery, put, delay } from 'redux-saga/effects'
import { increment } from '../actions/counter.action'
import { INCREMENT_ASYNC } from '../const.js/counter.const'
import { SHOWMODAL_ASYNC } from "../const.js/modal.const"
import {showModal, showModal_async} from "../actions/modal.action"

// takeEvery 接收action
// put 触发action

// 触发的action函数
function *increment_async_fn(action) {
  // 实现延迟效果
  yield delay(2000)
  // 使用put方法触发aaction 触发increment当中的action并传参
  yield put(increment(action.payload))
}

function *showModal_async_fn() {
  yield delay(2000)
  yield put(showModal())
}

// redux-saga 独立出来的函数 默认返回一个generator函数  
// 里面takeEvery接收action  第一个参数为要执行的action名称的字符串  第二个参数为要触发actioin函数
export default function *counterSaga() {
  yield takeEvery(INCREMENT_ASYNC, increment_async_fn)
  yield takeEvery(SHOWMODAL_ASYNC, showModal_async_fn)
}